// pages/publish/publish.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        radioGender: 1,
        show: false,
        minDate: new Date(1950,1,1).getTime(),
        currentDate: new Date().getTime(),
        optionNum: 1,  // 1代表出生日期, 0代表丢失日期
        showCutImage: false,
        image_url: "",
        lost:{
            name: "",
            gender: "",
            origin: "",
            birthday: "",
            lostDate: "",
            lostAddress: "",
            description: "",
            phone: "",
            email: "",
            other: "",
            imageUrl: ""
        },

    },
    returnImageUrl(e){
        this.setData({
            showCutImage: false
        })
        this.upload_file(e.detail.imageUrl);
        // console.log(e);
    },
    uploadHeadImg(){
        var a = this;
        wx.showActionSheet({
          itemList: ["从相册中选择", "拍照"],
          itemColor: "#f7982a",
          success:function(e){
              e.cancel || (0 == e.tapIndex ? a.chooseWxImageShop("album") : 1 == e.tapIndex && a.chooseWxImageShop("camera"));
          }
        })
    },
    chooseWxImageShop(a){
        var e = this;
        wx.chooseImage({
          sizeType: 'compressed',
          count: 1,
          sourceType: [a],
          success: function(a){
              if (a.tempFiles[0].size > 2097152){
                  wx.showModal({
                    title: "提示",
                    cancelColor: '',
                    content: "选择的图片过大， 请不要上传超过2M的图片",
                    showCancel: !1,
                    success: function(a){
                        a.confirm;
                    }
                  })
              }else{
                  e.setData({
                      image_url: a.tempFilePaths[0],
                      showCutImage: true
                  })
              }
          }
        })
    },

    upload_file(e){
        var _this = this;
        wx.showLoading({
          title: '上传中...',
        });
        wx.uploadFile({
          filePath: e,
          name: 'file',
          url: 'https://lzpi1.grass.wang/image/uploadImage?userAccount=18530794371',
          header: {
              "Content-Type": "multipart/form-data"
          },
          success: (a) => {
              const res = JSON.parse(a.data);
              console.log(res);
              if (res.code === 200){
                wx.hideLoading();
                console.log(res.data);
                // _this.modifyAvatar(res.data);
                _this.setData({
                    ['lost.imageUrl'] : res.data,
                    image_url: res.data
                })
                wx.showToast({
                    title: '上传成功',
                    icon: "success",
                    duration: 1500
                });
              }else{
                wx.hideLoading();
                wx.showToast({
                    title: '上传失败',
                    icon: "error",
                    duration: 3000
                });
              }
              

          },
          fail:function(a){
            wx.hideLoading();
            wx.showToast({
                title: '上传失败',
                icon: "error",
                duration: 3000
            });
          }
        })
        // console.log(this.data.lost);
    },


    // 检测必填选项是否填写
    checkAnyIsEmpty(){
        if(this.data.lost.name === null || this.data.lost.name === ""){
            wx.showToast({
              title: '名字为必填选项',
              icon: 'error',
              duration: 1500
            })
            return true;
        }
        else if(this.data.lost.gender === null || this.data.lost.gender === ""){
            wx.showToast({
              title: '性别为必填选项',
              icon: 'error',
              duration: 1500
            })
            return true;
        }
        else if(this.data.lost.origin === null || this.data.lost.origin === ""){
            wx.showToast({
              title: '籍贯为必填选项',
              icon: 'error',
              duration: 1500
            })
            return true;
        }
        // else if(this.data.lost.birthday === null || this.data.lost.birthday === ""){
        //     wx.showToast({
        //       title: '出生日期为必填选项',
        //       icon: 'error',
        //       duration: 1500
        //     })
        //     return true;
        // }
        // else if(this.data.lost.lostData === null || this.data.lost.lostData === ""){
        //     wx.showToast({
        //       title: '失踪日期为必填选项',
        //       icon: 'error',
        //       duration: 1500
        //     })
        //     return true;
        // }
        else if(this.data.lost.lostAddress === null || this.data.lost.lostAddress === ""){
            wx.showToast({
              title: '失踪地点为必填选项',
              icon: 'error',
              duration: 1500
            })
            return true;
        }
        else if(this.data.lost.description === null || this.data.lost.description === ""){
            wx.showToast({
              title: '相貌特征为必填选项',
              icon: 'error',
              duration: 1500
            })
            return true;
        }
        else if(this.data.lost.phone === null || this.data.lost.phone === ""){
            wx.showToast({
              title: '联系电话为必填选项',
              icon: 'error',
              duration: 1500
            })
            return true;
        }

        return false;

    },

    // 提交表单
    submitInformation(){
        // console.log(this.data.lost);
        if(this.checkAnyIsEmpty() == true) return;
        var _this = this
        wx.request({
          url: app.globalData.URL_PRE  + '/publishInformation',
          method: "POST",
          data:JSON.stringify(_this.data.lost),
          header: {
            "cookie": wx.getStorageSync('cookie'),
            // "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
            'content-type': 'application/json'
          },
            success: (res)=>{
                console.log(res.data);
                // 将数据清空
                this.setUserInfoIsEmpty();
                wx.navigateTo({
                  url: '/pages/detailPublish/detailPublish?lostId=' + res.data.data.lostId + '&whereFrom=publish',
                })
            }
            

        })

    },
    setUserInfoIsEmpty(){
        this.setData({
            ['lost.name']:"",
            ['lost.gender']:"",
            ['lost.birthday']:"",
            ['lost.lostDate']:"",
            ['lost.lostAddress']:"",
            ['lost.description']:"",
            ['lost.email']:"",
            ['lost.other']:"",
            ['lost.imageUrl']:"",
            ['lost.origin']:"",
            ['lost.phone']:"",
            radioGender: 0,
        })
    },
  
    onClickBirthday(){
        this.setData({
            show: true,
            optionNum: 1
        })
    },
    onClickLostDate(){
        this.setData({
            show: true,
            optionNum: 2
        })
    },
    
    onClose() {
        this.setData({ show: false })
    },
    formatDate (date) {  
        var y = date.getFullYear();  
        var m = date.getMonth() + 1;  
        m = m < 10 ? '0' + m : m;  
        var d = date.getDate();  
        d = d < 10 ? ('0' + d) : d;  
        return y + '-' + m + '-' + d;  
    },
    onConfirm(event) {
        // console.log(event.detail);
        var mdate = new Date(event.detail);
        // console.log(mdate.toISOString().slice(0, 10));
        var thisDate = this.formatDate(mdate);
        
        this.setData({ 
            show: false,
        })
        if(this.data.optionNum === 1){
            this.setData({ ['lost.birthday']: thisDate })
        }else{
            this.setData({ ['lost.lostDate']: thisDate })
        }
    },
    onCancel(){
        this.setData({ show: false})
    },
    
    
    onChangeGender(event){
        this.setData({
            radioGender: event.detail,
        })
        if(event.detail == 1){
            this.setData({
                ['lost.gender']: '男',
            })
        }
        else{
            this.setData({
                ['lost.gender']: '女',
            })
        }
        // console.log(event.detail);
        // console.log(this.data.lost.gender);
    },
    
    changeName(event){
        this.setData({
            ['lost.name']: event.detail.value
        })
        // console.log(event.detail.value);
    },
    changeOrigin(event){
        this.setData({
            ['lost.origin']: event.detail.value
        })
        // console.log(event.detail.value);
    },
    changeLostAddress(event){
        this.setData({
            ['lost.lostAddress']: event.detail.value
        })
        // console.log(event.detail.value);
    },
    changeDescription(event){
        this.setData({
            ['lost.description']: event.detail.value
        })
        // console.log(event.detail.value);
    },
    changePhone(event){
        this.setData({
            ['lost.phone']: event.detail.value
        })
        // console.log(event.detail.value);
    },
    changeEmail(event){
        this.setData({
            ['lost.email']: event.detail.value
        })
        // console.log(event.detail.value);
    },
    changeOther(event){
        this.setData({
            ['lost.other']: event.detail.value
        })
        // console.log(event.detail.value);
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})